package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.JCInfo.SPInfo;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.lottery.zc.tz_dialog.ZCBF_infor;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 初竞彩让球和胜平负 之外需要弹框选择比赛的Adapter
 *
 * @author miao
 */
public class JCZQ_Extend_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {

    private static final String TAG = "JCBF_ZhiFuAdapter";


    private int index;
    private boolean isDan;
    private List<JCInfo> infors;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;
    private OnFootballPayTeamDeleteListener listener;
    private int danCount = 0;
    private String chuanStr = JC_GuoGuanDialog.result;
    private int color_hui;
    private boolean isHG; // 是否是混合过关

    public JCZQ_Extend_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler
            handler, OnFootballPayTeamDeleteListener listener) {
        this.isDan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        this.listener = listener;
        color_hui = context.getResources().getColor(R.color.gray);
    }

    public JCZQ_Extend_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler
            handler, OnFootballPayTeamDeleteListener listener, boolean isHG) {
        this.isDan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        this.listener = listener;
        color_hui = context.getResources().getColor(R.color.gray);
        this.isHG = isHG;
    }

    @Override
    public int getCount() {

        return infors.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        if (position >= infors.size()) {
            return null;
        }
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (position >= infors.size()) {
            return 0;
        }
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        index = position;
        if (!chuanStr.equals(JC_GuoGuanDialog.result)) {
            for (int i = 0; i < infors.size(); i++) {
                infors.get(i).setDan(false);
            }
            chuanStr = JC_GuoGuanDialog.result;
        }
        convertView = mInflater.inflate(R.layout.jc_zhifu_listview_item_extend, null);
        final ViewHolder holder = new ViewHolder();
        holder.zhu = (LinearLayout) convertView.findViewById(R.id.jcbf_zhifu_list_item_zhu);
        holder.delete = (ImageView) convertView.findViewById(R.id.jcbf_zhifu_list_item_delete);
        holder.home = (TextView) convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_home);
        holder.week_num = (TextView) convertView.findViewById(R.id.week_num);
        holder.away = (TextView) convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_away);
        holder.bf = (TextView) convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_bf);
        holder.dan = (CheckBox) convertView.findViewById(R.id.jcbf_zhifu_list_item_dan);
        convertView.setTag(holder);

        if (isDan || isHG) {
            holder.dan.setVisibility(View.GONE);
        }
        if (index == infors.size()) {
            holder.zhu.setVisibility(View.GONE);
        } else {
            final JCInfo infor = infors.get(position);
            holder.zhu.setVisibility(View.VISIBLE);
            holder.home.setText(infor.getHomeTeam());
            holder.away.setText(infor.getAwayTeam());
            holder.bf.setText(infor.getResult());
            holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor
                    .getMatchNum()));

            danCount = getDanCount();

            holder.dan.setChecked(infor.isDan());
            if (danCount <= 0 && !infor.isDan()) {
                holder.dan.setEnabled(false);
                holder.dan.setTextColor(color_hui);
            } else {
                holder.dan.setEnabled(true);
            }

            jiSuanZhuShu();
            holder.delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    infor.setOnclikNum(0);

                    if (infor.btnHGPojo.containsKey(infor.VS_3)) {
                        infor.btnHGPojo.get(infor.VS_3).isSel = false;
                        infor.btnHGPojo.get(infor.VS_1).isSel = false;
                        infor.btnHGPojo.get(infor.VS_0).isSel = false;
                    } else if (infor.btnHGPojo.containsKey(infor.LETVS_3)) {
                        infor.btnHGPojo.get(infor.LETVS_3).isSel = false;
                        infor.btnHGPojo.get(infor.LETVS_1).isSel = false;
                        infor.btnHGPojo.get(infor.LETVS_0).isSel = false;
                    }

                    Set<Map.Entry<String, SPInfo>> set = infor
                            .btnHGPojo.entrySet();
                    for (Iterator<Map.Entry<String, SPInfo>> it = set
                            .iterator(); it.hasNext(); ) {
                        Map.Entry<String, SPInfo> entry = (Map.Entry<String, SPInfo>) it
                                .next();
                        SPInfo pojo = entry.getValue();
                        pojo.isSel = false;

                    }
                    List<ZCBF_infor> touZhu = infor.getTouZhuInfor();
                    for (int j = 0; j < touZhu.size(); j++) {
                        touZhu.get(j).setSelect(false);

                    }
                    if (listener != null) {
                        listener.onDelete((JCInfo) (v.getTag()));
                    }
                    infor.setResult(RESULT_MOREN);
                    setInfors(infors, isDan);
                    jiSuanZhuShu();
                    notifyDataSetChanged();
                }
            });
            holder.dan.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    infor.setDan(!infor.isDan());
                    notifyDataSetChanged();
                }
            });

        }

        return convertView;
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        List<JCInfo> list = new ArrayList<JCInfo>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (!info.getResult().equals(RESULT_MOREN)) {
                list.add(info);
            }
        }
        return list;
    }

    /**
     * 计算注数（待修改）
     */
    public void jiSuanZhuShu() {

        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            int xuan_count = 0;
            if (!info.getResult().equals(RESULT_MOREN)) {
                changCi++;
                String result = info.getResult();
                if (result.contains(RESULT_SPLIT)) {
                    isDanShi = false;
                    String[] split = result.split(RESULT_SPLIT);
                    xuan_count = split.length;
                } else {
                    xuan_count = 1;
                }
            }

            clicknums.add(xuan_count);
            if (info.isDan()) {
                wanFa = 2;
            }
            dannums.add(info.isDan());// 添加胆拖
        }
        List<Integer> chuans = new ArrayList<Integer>();
//		JC_GuoGuanDialog.setResult1(changCi);
        if (isDan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;
            if (chuan.contains("串")) {
                String[] chuan_array = chuan.split(",");
                for (int i = 0; i < chuan_array.length; i++) {
                    String chuan_i = chuan_array[i];
                    chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                }
            }
            for (Integer i : chuans) {
                zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }

    public List<JCInfo> getInfors() {
        return infors;
    }

    public void setInfors(List<JCInfo> infors, boolean isdanguan) {
        this.isDan = isdanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
    }

    private int getDanCount() {
        int danCount = 0;
        List<Integer> chuans = new ArrayList<Integer>();
        String chuan = JC_GuoGuanDialog.result;
        if (chuan.contains("串")) {
            String[] chuan_array = chuan.split(",");
            for (int i = 0; i < chuan_array.length; i++) {
                String chuan_i = chuan_array[i];
                chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
            }
        }
        if (chuans.size() > 0 && chuans.get(chuans.size() - 1) != infors.size()) {
            danCount = chuans.get(0) - 1;
        } else {
            danCount = 0;
        }
        for (int i = 0; i < infors.size(); i++) {
            if (infors.get(i).isDan()) {
                danCount--;
            }
        }
        return danCount;
    }

    class ViewHolder {
        LinearLayout zhu;
        LinearLayout wei;
        ImageView delete;
        TextView week_num;
        TextView home;
        TextView away;
        TextView bf;
        CheckBox dan;
    }

}
